const fs = require('fs');
// 从script2.js导入教案数据
const { lessonPlan, teachingProcess, reflection } = require('./script.js');

// 读取 HTML 模板
let html = fs.readFileSync('index2.html', 'utf8');

// 替换基本信息
html = html.replace('<td id="unit-name"></td>', `<td class="label-content">${lessonPlan['教学单元']}</td>`);
html = html.replace('<td id="plan-number"></td>', `<td class="label-content">${lessonPlan['教案编号']}</td>`);
html = html.replace('<td id="course-name"></td>', `<td class="label-content">${lessonPlan['课程名称']}</td>`);
html = html.replace('<td id="course-hours"></td>', `<td class="label-content">${lessonPlan['学时安排']}</td>`);
html = html.replace('<td id="class-name"></td>', `<td class="label-content">${lessonPlan['授课班级']}</td>`);
html = html.replace('<td id="location"></td>', `<td class="label-content">${lessonPlan['授课地点']}</td>`);
html = html.replace('<td id="week-number"></td>', `<td class="label-content">${lessonPlan['授课周数']}</td>`);
html = html.replace('<td id="technology"></td>', `<td class="label-content">${lessonPlan['使用技术']}</td>`);

// 替换教学内容
function createList(items) {
    return `<ul>${items.map(item => `<li>${item}</li>`).join('')}</ul>`;
}

html = html.replace('<div id="pre-class"></div>', `<div id="pre-class">${createList(lessonPlan['教学内容']['课前任务'])}</div>`);
html = html.replace('<div id="in-class"></div>', `<div id="in-class">${createList(lessonPlan['教学内容']['课中任务'])}</div>`);
html = html.replace('<div id="after-class"></div>', `<div id="after-class">${createList(lessonPlan['教学内容']['课后任务'])}</div>`);
html = html.replace('<div id="ideological"></div>', `<div id="ideological">${createList(lessonPlan['教学内容']['课程思政'])}</div>`);

// 替换学情分析
html = html.replace('<div id="knowledge-base"></div>', `<div id="knowledge-base">${createList(lessonPlan['具体学情分析']['知识和技能基础'])}</div>`);
html = html.replace('<div id="cognitive-ability"></div>', `<div id="cognitive-ability">${createList(lessonPlan['具体学情分析']['认知和实践能力'])}</div>`);
html = html.replace('<div id="learning-features"></div>', `<div id="learning-features">${createList(lessonPlan['具体学情分析']['学习特点'])}</div>`);
html = html.replace('<div id="information-literacy"></div>', `<div id="information-literacy">${createList(lessonPlan['具体学情分析']['信息素养'])}</div>`);

// 替换教学目标
html = html.replace('<div id="quality-goals"></div>', `<div id="quality-goals">${createList(lessonPlan['教学目标']['素质目标'])}</div>`);
html = html.replace('<div id="knowledge-goals"></div>', `<div id="knowledge-goals">${createList(lessonPlan['教学目标']['知识目标'])}</div>`);
html = html.replace('<div id="ability-goals"></div>', `<div id="ability-goals">${createList(lessonPlan['教学目标']['能力目标'])}</div>`);

// 替换教学方法与学法
html = html.replace('<div id="teaching-method"></div>', `<div>${lessonPlan['教法']}</div>`);
html = html.replace('<div id="learning-method"></div>', `<div>${lessonPlan['学法']}</div>`);

// 替换教学重难点
html = html.replace('<div id="key-points"></div>', `<div id="key-points">${createList(lessonPlan['教学重点'])}</div>`);
html = html.replace('<div id="difficulties"></div>', `<div id="difficulties">${createList(lessonPlan['教学难点'])}</div>`);
html = html.replace('<div id="key-solutions"></div>', `<div id="key-solutions">${createList(lessonPlan['教学重点解决办法'])}</div>`);
html = html.replace('<div id="difficulty-solutions"></div>', `<div id="difficulty-solutions">${createList(lessonPlan['教学难点解决办法'])}</div>`);

// 替换教学环节
html = html.replace('<div id="pre-guidance"></div>', `<div id="pre-guidance">${createList(lessonPlan['环节']['课前导学'])}</div>`);
html = html.replace('<div id="in-class-exploration"></div>', `<div id="in-class-exploration">${createList(lessonPlan['环节']['课中探知'])}</div>`);
html = html.replace('<div id="after-class-extension"></div>', `<div id="after-class-extension">${createList(lessonPlan['环节']['课后拓展'])}</div>`);

// 创建教学过程表格内容
function createTeachingProcessTable(phase, phaseData) {
    let tableHtml = '';
    Object.entries(phaseData).forEach(([topic, topicData]) => {
        Object.entries(topicData).forEach(([task, taskData], taskIndex) => {
            tableHtml += '<tr>';
            if (taskIndex === 0) {
                tableHtml += `
                    <td rowspan="${Object.keys(topicData).length}" class="label">${phase}</td>
                    <td rowspan="${Object.keys(topicData).length}" class="label">${topic}</td>
                `;
            }
            tableHtml += `
                <td class="label">${task}</td>
                <td class="label-content inner">${taskData['教师活动']}</td>
                <td class="label-content inner">${taskData['学生活动']}</td>
                <td class="label-content inner">${taskData['教学资源技术手段']}</td>
                <td class="label-content inner">${taskData['设计意图']}</td>
            </tr>`;
        });
    });
    return tableHtml;
}

// 替换课前导学表格内容
const preClassTable = createTeachingProcessTable('课前导学', teachingProcess['课前导学']);
html = html.replace(/<tr>\s*<td rowspan="3" class="label">课前导学[\s\S]*?<\/tr>\s*<tr>\s*<td class="label">分析任务[\s\S]*?<\/tr>\s*<tr>\s*<td class="label">阶段测试[\s\S]*?<\/tr>/, preClassTable);

// 替换课中探知表格内容
const inClassTable = createTeachingProcessTable('课中探知', teachingProcess['课中探知']);
html = html.replace(/<tr>\s*<td rowspan="2" class="label">课中探知[\s\S]*?<\/table>\s*<table class="info-table">/, `${inClassTable}</table>\n\n<table class="info-table">`);

// 替换反思思政表格内容
const reflectionHtml = `
    <tr>
        <td class="label">教学成效</td>
        <td class="label-content inner">${reflection['教学成效'].map(item => `<div>${item}</div>`).join('')}</td>
    </tr>
    <tr>
        <td class="label">问题与改进</td>
        <td class="label-content inner">${reflection['问题与改进'].map(item => `<div>${item}</div>`).join('')}</td>
    </tr>
`;
html = html.replace(/<tr>\s*<td class="label">教学成效[\s\S]*?<\/tr>\s*<tr>\s*<td class="label">问题与改进[\s\S]*?<\/tr>/, reflectionHtml);

// 写入渲染后的文件
fs.writeFileSync('rendered.html', html);